package com.ruoyi.client.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.annotation.Client_oper;
import com.ruoyi.client.annotation.TargetJson;
import com.ruoyi.client.domain.entity.BlogCollect;
import com.ruoyi.client.domain.vo.EchartsVo;
import com.ruoyi.client.eum.ClientBusinessType;
import com.ruoyi.client.service.IBlogCollectService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequestMapping("client/user/collect")
public class BlogCollectController extends BaseController {
    @Autowired
    IBlogCollectService collectService;
    @GetMapping("addCollectNum")
    public R<List<EchartsVo>> getAddCommentNum(){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取7天前的日期
        LocalDate sevenDaysAgo = today.minusDays(6); // 包括今天，所以减去6天
        // 获取最近7天的开始时间（7天前的 00:00:00）
        LocalDateTime startOfLastSevenDays = sevenDaysAgo.atStartOfDay();
        // 获取最近7天的结束时间（今天的 23:59:59.999999999）
        LocalDateTime endOfLastSevenDays = today.atTime(LocalTime.MAX);
        List<EchartsVo> result = collectService.addLikeNum(userId,startOfLastSevenDays,endOfLastSevenDays);
        return R.ok(result);
    }
    /**
     * 删除收藏记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        collectService.removeById(id);
        return R.ok();
    }
    /**
     * 收藏
     * @param
     * @return
     */
    @PostMapping
    @Client_oper(title = "收藏",clientBusinessType = ClientBusinessType.TYPE_COLLECTION)
    public R collectArticle(@RequestBody @TargetJson BlogCollect collect){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        collect.setUserId(userId);
        collectService.collectArticle(collect,collect.getIsCollect());
        return toAjax(true);
    }
}
